How to Center Align a Table with CSS? 您所在的位置:网站首页 animated login page using html css javascript How to Center Align a Table with CSS?

How to Center Align a Table with CSS?

#How to Center Align a Table with CSS?| 来源: 网络整理| 查看: 265

In HTML, tables are used to display data in a structured format. They are commonly used to display data that would be difficult to read in a simple list or paragraph format.

When you add a table to your HTML page, it is by default aligned to the left side of the page or its containing element. But many times we need to change its default alignment to meet the design requirements of the application.

In old days, we used to add the align="center" attribute to the itself to align it to the center. But, the align attribute is now deprecated and we shouldn’t use it. Instead, we should use CSS for aligning and styling purposes.

In CSS there are a number of ways that you can use to center align a table. Some of the commonly used methods are as follows:

Center table using the margin autoCenter table using flexboxCenter table using the position property

Let’s discuss each method in detail.

Center Table Using the Margin Auto

The is a block-level HTML element. Therefore if you set the left and right margins of the table to the same value auto, it will be automatically aligned to the center of its containing element.

Setting the margin to the value auto means that the browser will itself calculate the left and right margins of the element.

The browser actually checks how much space is available after placing the element on the layout and divides the available space equally between the left and right margins. Therefore, the element gets centered horizontally.

First Name Last Name Age John Doe 21 Will Smith 24 Jack Sparrow 30

Add the CSS:

table{ margin-left: auto; margin-right: auto; } table, th, td{ border: 1px solid grey; border-collapse: collapse; padding: 5px; }

Output:Center table using margin auto in CSS

In the above example, we didn’t specify the width of the table.

But if you specify the width of the table and set the margin-left and margin-right to auto, still the table will be center aligned horizontally.

That’s because when you set the width to some fixed value say 60%, and set the left and right margins to auto, the remaining 40% of space is equally divided between the left and right margins and therefore the table gets centered horizontally.

table{ width: 60%; margin-left: auto; margin-right: auto; text-align: center; } table, th, td{ border: 1px solid grey; border-collapse: collapse; padding: 5px; }

Output:

Center fixed with table with margin auto in CSSCenter Table using CSS Flexbox(The Modern Way)

The flexbox module in CSS is a modern way to create responsive and flexible layouts. You can also use it to align items horizontally as well as vertically.

To align a table to the center of the page or its container with the flexbox module, you have to wrap it with a block-level element such as a div and then make this div a flex container by setting its display property to flex.

Now, to center align the table horizontally, you can set the justify-content property to center and to center align vertically, you can set the align-items property to center.

First Name Last Name Age John Doe 21 Will Smith 24 Jack Sparrow 30

Add the CSS:

div { display: flex; /* Make flex container */ justify-content: center; /* Center horizontally */ } table { width: 60%; text-align: center; /* center text */ } table, th, td { border: 1px solid grey; border-collapse: collapse; padding: 5px; }

Output:Center align a table with CSS flexbox

Center Table using Position Property

The position property in CSS is used to control the positioning of an element on the webpage. You can easily use this property to center align a table horizontally and vertcally both.

To center align a table with the position property, you have to first wrap it with a block-level element such as a div and then set the position of this div to relative.

Setting the position of the div to relative makes sure that all of its child elements will be positioned relative to itself not to the viewport.

Now, set the position of the table to absolute and apply left: 50%; on it. This will put only the left side of the table in the center of its container. Therefore, we have to pull the whole table by -50% using the transform property so that it sits in the exact center of the div.

First Name Last Name Age John Doe 21 Will Smith 24 Jack Sparrow 30

Add the CSS:

div { position: relative; } table { position: absolute; left: 50%; /* Put table's left side in center */ transform: translateX(-50%); /* Pull whole table by -50% */ width: 50%; text-align: center; /* center text */ } table, th, td { border: 1px solid grey; border-collapse: collapse; padding: 5px; }

Output:Center table using the position property

Conclusion

In this article, we learned different ways of centering a table with CSS.

In summary, to center align a table in CSS, you can set the left and right margins of the table to auto.

When you set the left and right margins of the table to auto, the browser automatically calculates the margins for the table and divides the available space equally between them. Therefore, the table automatically gets aligned to the center of the page or its container.

You can also use other methods such as the flexbox module or the position property to align the table to the center horizontally.

Thanks for reading.

Related posts:Create previous and next buttons in HTML & CSSHow to Make a Navbar Transparent using CSS?How to Change the Selected Option Color using CSS?How to Fit an Image in a Circle using CSS?How to Select Last Row in a Table using CSS?How to Select All Child Elements using CSS?How to Put a Box Inside Another Box using CSS?Ezoicreport this ad


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有